<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="岗位名称" prop="postShowName">
        <el-input
          v-model="queryParams.postShowName"
          placeholder="请输入部门岗位显示名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:deptpost:setting']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleSetting"
        >
          岗位设置
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:deptpost:delete']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          删除
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="deptPostList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" label="岗位编号" prop="id" />
      <el-table-column align="center" label="岗位名称" prop="postName" />
      <el-table-column label="岗位别名" align="center" prop="postShowName" />
      <el-table-column align="center" label="岗位层级" prop="level" />
      <el-table-column align="center" label="领导岗位" prop="leaderPost" width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.leaderPost===1" type="success">
            是
          </el-tag>
          <el-tag v-else type="danger">
            否
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:deptpost:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改别名
          </el-button>
          <el-button
            v-hasPermi="['system:deptpost:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="deptPostTotal>0"
      :total="deptPostTotal"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改部门岗位关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="岗位别名" prop="postShowName">
          <el-input v-model="form.postShowName" placeholder="请输入岗位别名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">
          确 定
        </el-button>
        <el-button @click="cancel">
          取 消
        </el-button>
      </div>
    </el-dialog>
    <!--    设置岗位-->
    <el-dialog title="部门岗位设置" :visible.sync="settingOpen" width="800px" append-to-body>
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="岗位名称" prop="postName">
          <el-input
            v-model="postQueryParams.postName"
            placeholder="请输入岗位名称"
            clearable
            size="small"
            @keyup.enter.native="handlePostQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handlePostQuery">
            搜索
          </el-button>
        </el-form-item>
      </el-form>
      <el-table ref="postTable" v-loading="loading" :data="postList" row-key="id">
        <el-table-column type="selection" width="55" align="center" reserve-selection />
        <el-table-column align="center" label="部门岗位编号" prop="id" />
        <el-table-column align="center" label="岗位编码" prop="postCode" />
        <el-table-column align="center" label="岗位名称" prop="postName" />
        <el-table-column align="center" label="岗位层级" prop="level" />
        <el-table-column align="center" label="领导岗位" prop="leaderPost" width="180">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.leaderPost===1" type="success">
              是
            </el-tag>
            <el-tag v-else type="danger">
              否
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="postTotal>0"
        :total="postTotal"
        :page.sync="postQueryParams.current"
        :limit.sync="postQueryParams.size"
        @pagination="getPostList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSettingDeptPost">
          确 定
        </el-button>
        <el-button @click="settingOpen=false">
          取 消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {delDeptPost, queryDeptNotInPost, settingDeptPost, updateDeptPost,pageDeptPost} from "../../../api/system/dept"

export default {
  name: "DeptPost",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      deptPostTotal: 0,
      // 部门岗位关系表格数据
      deptPostList: [],
      // 部门列表
      postList: [],
      postTotal: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //  设置岗位
      settingOpen: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        deptId: null,
        postShowName: null
      },
      // 查询参数
      postQueryParams: {
        current: 1,
        size: 10,
        deptId: null,
        postName: ''
      },
      // 问卷参数
      form: {},
      // 问卷校验
      rules: {
        deptId: [
          {required: true, message: "部门Id不能为空", trigger: "blur"}
        ],
        postId: [
          {required: true, message: "岗位Id不能为空", trigger: "blur"}
        ],
        postShowName: [
          {required: true, message: "部门岗位显示名称不能为空", trigger: "blur"}
        ]
      }
    }
  },
  mounted() {
    this.queryParams.deptId = this.$route.query.id || null
    this.getList()
  },
  methods: {
    /** 查询部门岗位关系列表 */
    getList() {
      this.loading = true
      pageDeptPost(this.queryParams).then((response) => {
        this.deptPostList = response.data.records
        this.deptPostTotal = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 问卷重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        postId: null,
        postShowName: null,
        updateTime: null,
        createTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 搜索按钮操作 */
    handlePostQuery() {
      this.postQueryParams.current = 1
      this.getPostList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = row
      this.open = true
      this.title = "修改岗位别名"
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateDeptPost(this.form).then((response) => {
              this.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          }
      })
    },
    getPostList() {
      this.postQueryParams.deptId = this.queryParams.deptId
      queryDeptNotInPost(this.postQueryParams).then((response) => {
        this.postList = response.data.records
        this.postTotal = response.data.total
      })
    },
    handleSetting() {
      this.settingOpen = true
      this.getPostList()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除部门岗位编号为"' + ids + '"的数据项？').then(function () {
        return delDeptPost(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 设置部门岗位 */
    handleSettingDeptPost() {
      if (this.$refs.postTable.selection.length === 0) {
        this.msgError("请选择岗位")
        return
      }
      let deptId = this.queryParams.deptId
      settingDeptPost({
        deptId,
        ids: this.$refs.postTable.selection.map((item) => item.id)
      }).then((response) => {
        this.msgSuccess("设置成功")
        this.settingOpen = false
        this.getList()
      })
    }
  }
}
</script>
